cmake_minimum_required(VERSION 3.12)
project(OrbbecSDK-Samples LANGUAGES CXX C)

# 设置C++标准
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置编译选项
IF(UNIX)
    set(CMAKE_CXX_FLAGS "-std=c++14 -pthread ${CMAKE_CXX_FLAGS}")
    set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wextra")
    set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
ENDIF()

# OrbbecSDK配置
set(OrbbecSDK_ROOT_DIR ${CMAKE_SOURCE_DIR}/../SDK)
set(OrbbecSDK_LIBRARY_DIRS ${OrbbecSDK_ROOT_DIR}/lib)
set(OrbbecSDK_INCLUDE_DIR ${OrbbecSDK_ROOT_DIR}/include)

# 包含目录
include_directories(${OrbbecSDK_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/scvOrbbecWrapper)

# 链接目录
link_directories(${OrbbecSDK_LIBRARY_DIRS})



set(OpenCV_DIR "/home/weiyixing/D/work/C_Project/sl_iris/3rdparty/opencv/linux/amd64/lib/cmake/opencv4")
# 查找OpenCV
if(NOT DEFINED OpenCV_DIR)
    if(DEFINED ENV{OpenCV_INSTALL_PATH})
        set(OpenCV_DIR $ENV{OpenCV_INSTALL_PATH})
    endif()
endif()

find_package(OpenCV REQUIRED)

if(DEFINED OpenCV_LIBS AND DEFINED OpenCV_INCLUDE_DIRS)
    set(OpenCV_FOUND TRUE)
    include_directories(${OpenCV_INCLUDE_DIRS})
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
else()
    set(OpenCV_FOUND FALSE)
    message(FATAL_ERROR "OpenCV not found")
endif()

# 设置输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Linux系统RPATH设置
if(UNIX AND NOT APPLE)
    set(CMAKE_SKIP_BUILD_RPATH FALSE)
    set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
    set(CMAKE_INSTALL_RPATH "$ORIGIN:${CMAKE_INSTALL_RPATH}:${OrbbecSDK_LIBRARY_DIRS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--disable-new-dtags")
endif()

## 构建C++示例
#add_subdirectory(cpp)

# SCV Orbbec Wrapper库
file(GLOB_RECURSE SCVW_SOURCES 
    ${CMAKE_CURRENT_SOURCE_DIR}/scvOrbbecWrapper/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/scvOrbbecWrapper/*.h
)

# 排除Python绑定文件（单独处理）
list(REMOVE_ITEM SCVW_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/scvOrbbecWrapper/pybind11_wrapper.cpp)

set(LINK_THIRD_LIBS ${OpenCV_LIBS})

# 创建C++库
add_library(SCV_ORBBEC_W SHARED ${SCVW_SOURCES})
target_include_directories(SCV_ORBBEC_W PUBLIC 
    ${CMAKE_CURRENT_SOURCE_DIR}/scvOrbbecWrapper
    ${OrbbecSDK_INCLUDE_DIR}
    ${OpenCV_INCLUDE_DIRS}
)
target_link_libraries(SCV_ORBBEC_W PUBLIC ${LINK_THIRD_LIBS} OrbbecSDK)

# 设置库的版本信息
set_target_properties(SCV_ORBBEC_W PROPERTIES
    VERSION 1.0.0
    SOVERSION 1
    PUBLIC_HEADER scvCameraCore.h
)

# Python绑定支持
option(BUILD_PYTHON_BINDINGS "Build Python bindings using pybind11" ON)

if(BUILD_PYTHON_BINDINGS)
    # 查找Python
    set(Python3_EXECUTABLE "/home/weiyixing/anaconda3/envs/python310/bin/python3.10")
    find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
    
    # 查找pybind11
    find_package(pybind11 REQUIRED)

    
    # 创建Python绑定
    pybind11_add_module(scvOrbbec
        ${CMAKE_CURRENT_SOURCE_DIR}/scvOrbbecWrapper/pybind11_wrapper.cpp
    )
    
    # 设置Python模块的包含目录和链接库
    target_include_directories(scvOrbbec PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/scvOrbbecWrapper
        ${OrbbecSDK_INCLUDE_DIR}
        ${OpenCV_INCLUDE_DIRS}
    )
    
    target_link_libraries(scvOrbbec PRIVATE
        SCV_ORBBEC_W
        ${LINK_THIRD_LIBS}
        OrbbecSDK
    )
    
    # 设置Python模块的编译选项
    target_compile_definitions(scvOrbbec PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO})
    
    # 安装Python模块
    install(TARGETS scvOrbbec
        COMPONENT python
        LIBRARY DESTINATION ${Python3_SITEARCH}
    )
    

    
    message(STATUS "Python bindings enabled")
    message(STATUS "Python executable: ${Python3_EXECUTABLE}")
    message(STATUS "Python version: ${Python3_VERSION}")
    message(STATUS "Python site-packages: ${Python3_SITEARCH}")
endif()

# 测试目录
add_subdirectory(test)

# 安装规则
install(TARGETS SCV_ORBBEC_W
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    PUBLIC_HEADER DESTINATION include
)

# 安装OrbbecSDK库文件
install(DIRECTORY ${OrbbecSDK_LIBRARY_DIRS}/
    DESTINATION lib
    FILES_MATCHING PATTERN "*.so*"
)

# 创建pkg-config文件
configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/scvOrbbecWrapper/scv-orbbec.pc.in
    ${CMAKE_BINARY_DIR}/scv-orbbec.pc
    @ONLY
)

install(FILES ${CMAKE_BINARY_DIR}/scv-orbbec.pc
    DESTINATION lib/pkgconfig
)

# 打印配置信息
message(STATUS "=== OrbbecSDK Samples Configuration ===")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "OrbbecSDK root: ${OrbbecSDK_ROOT_DIR}")
message(STATUS "OpenCV found: ${OpenCV_FOUND}")
message(STATUS "OpenCV version: ${OpenCV_VERSION}")
message(STATUS "Python bindings: ${BUILD_PYTHON_BINDINGS}")
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "========================================")